import Raphael from 'raphael'

const l = {
  x: 0,
  y: 0,
  x2: 0,
  y2: 0,

}

class Line {
  constructor(conf) {
	this.conf = conf;
	// paper
	this.r = conf.r;
	// 线段开始和结束时,距离元素的X轴临边长度
	this.nearSide = 30;
	this.lineObj = null;
	this.fromNode = conf.fromNode || null;
	this.toNode = conf.toNode || null;
	this.lineStyle = 'solid';
  }

  getTanDeg(deg) {
	var rad = deg * Math.PI / 180;
	return Math.tan(rad);
  }

  /**
   * render line
   */
  render() {
	let r = this.conf.r;
	// 起点中心点
	let startPoint = {
	  x: this.fromNode.nodeImg.attr("x") + 15,
	  y: this.fromNode.nodeImg.attr("y") + 15,
	};
	// 终点中心点
	let endPoint = {
	  x: this.toNode.nodeImg.attr("x") + 15,
	  y: this.toNode.nodeImg.attr("y") + 15,
	};
	// let angle = Raphael.angle(startPoint.x,
	// 	startPoint.y,
	// 	endPoint.x,
	// 	endPoint.y);
	// let linePosition = {
	// 	start : {
	// 		x : startPoint.x + this.nearSide ,
	// 		y : startPoint.y - Math.tan(this.getTanDeg(angle))*this.nearSide,
	// 	} ,
	// 	end : {
	// 		x : endPoint.x - this.nearSide ,
	// 		y : endPoint.y +  Math.tan(this.getTanDeg(angle))*this.nearSide ,
	// 	} ,
	// };
	// let arrowLine = r.path("M" + linePosition.start.x + " " + linePosition.start.y + "L" + linePosition.end.x + " " + linePosition.end.y);
	let arrowLine = r.path("M" + this.fromNode.slot.right.x + " " + this.fromNode.slot.right.y + "L" + this.toNode.slot.left.x + " " + this.toNode.slot.left.y);
	// 如果线的开始节点的类型为鉴别或者唯一，为虚线 nodeType : 1 ,2
	// 如果线的开始节点的类型为并发，为实线 nodeType : 0
	arrowLine.attr({
	  'stroke-width': 1.5,
	  'stroke': '#444444',
	  'stroke-dasharray': (this.fromNode.precondition === '1' || this.fromNode.precondition === '2') ? '-' : '',
	  'arrow-end': 'classic-wide-long',
	});
	this.lineObj = arrowLine;
	return arrowLine;
  }

  update() {
	this.lineObj.remove();
	this.render();
  }

  remove() {
	this.lineObj.remove();
	// 清除自己
  }
}

export default Line;
